<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-button type="primary" @click="showDialog=true">+ 新增角色</el-button>
            <el-table
              :data="rolesList"
              style="width: 100%"
            >
              <el-table-column
                label="序号"
                type="index"
                width="200"
              >
                <template slot-scope="{$index}">
                  <!-- {{ $index+1 }}
                  {{ q.page }}
                  {{ q.pagesize }} -->
                  {{ ($index+1) + (q.page-1) * q.pagesize }}
                </template>
              </el-table-column>
              <el-table-column
                prop="name"
                label="角色名称"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column label="操作">
                <template slot-scope="{row}">
                  <el-button
                    size="mini"
                    type="success"
                    @click="permissonBtn(row)"
                  >分配权限</el-button>
                  <el-button
                    size="mini"
                    @click="Erole(row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="Drole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" style="margin-top:15px">
              <el-pagination
                :page-size="q.pagesize"
                :current-page="q.page"
                layout="total,prev, pager, next"
                :total="total"
                @current-change="hCurrentChange"
              />
            </el-row>
            <!-- 新增弹框 -->
            <el-dialog
              :title="cTitle"
              :before-close="dialogClose"
              :close-on-click-modal="false"
              :close-on-press-escape="false"
              :visible.sync="showDialog"
              :v-if="showDialog"
            >
              <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="6">
                  <el-button size="small" @click="cancel">取消</el-button>
                  <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="公司设置" name="second">
            <el-form ref="companyform" :model="companyform" label-width="180px" disabled style="padding-right:800px">
              <el-form-item label="企业名称">
                <el-input v-model="companyform.name" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyform.companyAddress" />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="companyform.companyPhone" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyform.mailbox" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyform.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane></el-tabs>
        <el-dialog
          v-if="perVisible"
          title="分配权限"
          :visible.sync="perVisible"
          width="50%"
          :before-close="perClose"
        >
          <per-dialog :roleid="roleid" @perClose="perClose" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getRoles, removeRoles, addRole, editRole, getCompany } from '@/api/settings'
import perDialog from './perDialog.vue'
export default {
  components: {
    perDialog
  },
  data() {
    return {
      showDialog: false,
      activeName: 'first',
      rolesList: [],
      q: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      roleForm: {
        name: '',
        description: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空不能为空', trigger: 'blur' }]
      },
      isEdit: false,
      companyform: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      perVisible: false,
      roleid: ''
    }
  },
  computed: {
    //   表格中最大的页码
    maxNum() {
      return Math.ceil(this.total / this.q.pagesize)
    },
    // 最后一页是不满的
    isLastPageFulled: function() {
      return this.total % this.q.pagesize === 0
    },
    cTitle() {
      return this.isEdit ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.initRoles()
    this.getCompanyInfo()
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row)
    },
    handleDelete(index, row) {
      console.log(index, row)
    },
    async initRoles() {
      const res = await getRoles(this.q)
      if (res.code === 10000) {
        this.rolesList = res.data.rows
        this.total = res.data.total
      }
    },
    hCurrentChange(c) {
      this.q.page = c
      this.initRoles()
    },
    Drole(id) {
      this.$confirm('亲，确定要删除该角色信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await removeRoles(id)
        if (res.code === 10000) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          if (this.rolesList.length === 1) {
            this.q.page--
            if (this.q.page <= 0) {
              this.q.page = 1
            }
          }
          this.initRoles()
        }
      }).catch(err => err)
    },
    Erole(row) {
      console.log(row)
      this.showDialog = true
      this.roleForm = JSON.parse(JSON.stringify(row))
      this.isEdit = true
    },
    hSubmit() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        if (this.isEdit) {
          const res = await editRole(this.roleForm)
          if (res.code === 10000) {
            this.$message({
              type: 'success',
              message: '修改角色信息成功'
            })
            this.isEdit = false
          }
        } else {
          const res = await addRole(this.roleForm)
          if (res.code === 10000) {
            this.$message({
              type: 'success',
              message: '添加角色信息成功'
            })
          }
          if (this.isLastPageFulled) {
            this.q.pagesize = this.maxNum + 1
            this.total++
          } else {
            //   想查看最新添加的数据，要调到最大页码
            this.q.page = this.maxNum
          }
        }
        this.cancel()
        this.initRoles()
      })
    },
    cancel() {
      this.isEdit = false
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: '',
        id: ''
      }
    },
    dialogClose() {
      this.cancel()
    },
    async getCompanyInfo() {
      const res = await getCompany()
      if (res.code === 10000) {
        this.companyform = res.data[0]
      }
    },
    permissonBtn(row) {
      this.roleid = row.id
      this.perVisible = true
    },
    // 分配权限框关闭
    perClose() {
      this.perVisible = false
    }
  }
}
</script>
